package cn.tedu.store.controller;

import cn.tedu.store.entity.Order;
import cn.tedu.store.service.IOrderService;
import cn.tedu.store.util.ResponseResult;
import cn.tedu.store.vo.OrderVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/order")
public class OrderController
	extends BaseController {

	@Autowired
	private IOrderService orderService;
	/**
	 *创建订单
	 * @param session
	 * @param addressId
	 * @param cartIds
	 * @return
	 */
	@RequestMapping("/create")
	public ResponseResult<Order> createOrder(
		HttpSession session, 
		@RequestParam("address") Integer addressId, 
		@RequestParam("cart_id") Integer[] cartIds) {
		Integer uid = getUidFromSession(session);
		String username = session.getAttribute("username").toString();
		Order order = orderService.createOrder(
			uid, username, addressId, cartIds);
		return new ResponseResult<Order>(SUCCESS, order);
	}

	/**
	 * 订单详情
	 * @param id
	 * @return
	 */
	@GetMapping("/details/{id}")
	public ResponseResult<OrderVO> getById(
		@PathVariable("id") Integer id) {
		OrderVO data = orderService.getById(id);
		return new ResponseResult<OrderVO>(
			SUCCESS, data);
	}

	/**
	 * 我的订单
	 * @param uid
	 * @return
	 */
	public ResponseResult<OrderVO> listOrders(
			@PathVariable("uid") Integer uid) {
		OrderVO data = orderService.getById(uid);
		return new ResponseResult<OrderVO>(
				SUCCESS, data);
	}

	
}




